package net.a6polo.renren.ui.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.a6polo.a6poloweibo.R;
import net.a6polo.a6poloweibo.util.AsynImageLoader;
import net.a6polo.renren.domain.FeedResponse;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RenrenTimelineListViewAdapter extends BaseAdapter {

	private List<FeedResponse> timeline;
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private Context context;

	public RenrenTimelineListViewAdapter(List<FeedResponse> timeline, Context context) {
		this.timeline = timeline;
		this.context = context;
	}
	
	public void addItem(List<FeedResponse> timeline) {
		
		this.timeline.addAll(timeline);
	}

	@Override
	public int getCount() {
		if (null != timeline) {
			return timeline.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		return timeline.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = rowViews.get(position);

		if (null == rowView) {

			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(
					R.layout.sina_timeline_listview_item, null);
			
			FeedResponse item = (FeedResponse) getItem(position);
			
			TextView weiboId = (TextView) rowView.findViewById(R.id.sinaWeiboId);
			weiboId.setText(item.getPost_id()+"");
			

			TextView userName = (TextView) rowView.findViewById(R.id.sinaWeiboUserName);
			TextView text = (TextView) rowView.findViewById(R.id.sinaWeiboText);			
			userName.setText(item.getName());
			
			String content = item.getPrefix() + "\n" + item.getTitle() + item.getMessage();
			
			if(item.getFeed_type() == 11) {
				content = item.getMessage();
			} else if(item.getFeed_type() == 10) {
				content = item.getPrefix() ;
			}else if(item.getFeed_type() == 22 || item.getFeed_type() == 20) {
				content = item.getPrefix() + "\n" + item.getTitle() ;
			}
			
			text.setText(content);
			

			TextView date = (TextView) rowView.findViewById(R.id.sinaWeiboDate);
//			Date datetime = new Date(item.getUpdate_time()); 
//			SimpleDateFormat sdf=new SimpleDateFormat("MM-dd HH:mm");  
//			String str = sdf.format(datetime);  
			date.setText(item.getUpdate_time());
			

			if(null != item.getSource()) {
				TextView from = (TextView) rowView.findViewById(R.id.txt_wb_item_from);
				from.setText(item.getSource().getSource_text());
				from.setMovementMethod(LinkMovementMethod.getInstance());
			}

			TextView redirect = (TextView) rowView.findViewById(R.id.txt_wb_item_redirect);
			TextView comment = (TextView) rowView.findViewById(R.id.txt_wb_item_comment);
			redirect.setText(item.getLikes().getTotal_count()+"");
			comment.setText(item.getComments().getCount()+"");
			

			AsynImageLoader loader = new AsynImageLoader();
			ImageView userIcon = (ImageView)rowView.findViewById(R.id.img_wb_item_head);
			Bitmap userHead = loader.loadDrawableFromNet(userIcon,item.getHeadurl());
			userIcon.setImageBitmap(userHead);

//			if(item.getUser().isVerified()) {
//				ImageView vUser = (ImageView)rowView.findViewById(R.id.img_wb_item_V);
//				vUser.setVisibility(ImageView.VISIBLE);
//			}
			

			if(null != item.getAttachment() && item.getAttachment().size() > 0) {
				ImageView content_Pic = (ImageView)rowView.findViewById(R.id.img_wb_item_content_pic);
				content_Pic.setVisibility(ImageView.VISIBLE);
				Bitmap contentPic = loader.loadDrawableFromNet(content_Pic,item.getAttachment().get(0).getSrc());
				content_Pic.setImageBitmap(contentPic);
			}
			
			
//			
//
//			if(null != item.getRetweeted_status()) {
//				LinearLayout item_sublayout = (LinearLayout)rowView.findViewById(R.id.lyt_wb_item_sublayout);
//				item_sublayout.setVisibility(LinearLayout.VISIBLE);
//				TextView subcontent = (TextView) rowView.findViewById(R.id.txt_wb_item_subcontent);
//				subcontent.setText(item.getRetweeted_status().getText());
//				
//				if(null != item.getRetweeted_status().getThumbnail_pic()) {
//					ImageView sub_content_Pic = (ImageView)rowView.findViewById(R.id.img_wb_item_content_subpic);
//					sub_content_Pic.setVisibility(ImageView.VISIBLE);
//					Bitmap contentPic = loader.loadDrawableFromNet(sub_content_Pic,item.getRetweeted_status().getThumbnail_pic());
//					sub_content_Pic.setImageBitmap(contentPic);
//					
//					
//				}
//			}
			

			rowViews.put(position, rowView);
			
			rowView.setOnClickListener(new OnClickListener()
	        {

	            @Override
	            public void onClick(View view)
	            {

	                
	            }
	            
	        });
		}

		return rowView;
	}

}
